package org.itboys.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.itboys.admin.common.SystemLoginUserPermission;
import org.itboys.admin.service.AdminUserService;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.interceptor.ServletContextInterceptor;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayMemberSessionDto;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.param.ParamUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by weisky on 2016/8/11.
 */
public class AdminIntercepter extends ServletContextInterceptor {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 是否开启接口的权限校验。
	 * 开启。自行删除后台角色管理页面。template\screen\ace\sys\role-list.vm中【分配菜单权限】按钮
	 * 关闭。自行删除后台角色管理页面。template\screen\ace\sys\role-list.vm中【分配功能权限】按钮
	 */
	public static boolean is_open_operate_permission_check=false;
    @Autowired
    private ResourceHolder resourceHolder;
  /*  @Autowired
    private AdminUserService adminUserService;*/

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {

        request.setAttribute("staticRoot", resourceHolder.getStringValue("staticRoot"));
        request.setAttribute("adminRoot", resourceHolder.getStringValue("adminRoot"));
        request.setAttribute("imgRoot", resourceHolder.getStringValue("imgRoot"));

        //2017年3月14日，江雄
		request.setAttribute("dictionaryRoot", resourceHolder.getStringValue("dictionaryRoot"));
		request.setAttribute("dictionaryRootMuilt", resourceHolder.getStringValue("dictionaryRootMuilt"));



		String url = request.getRequestURI();
		logger.info("本次请求地址="+url);
		//获取请求参数
		ParamUtil.getRequestParam(request);
		/**校验接口权限**/
		if(is_open_operate_permission_check){
			System.err.print("本次用户权限"+ ListUtil.isNotNull(SystemLoginUserPermission.loginUserOperatePermssion));
			if(ListUtil.isNotNull(SystemLoginUserPermission.loginUserOperatePermssion)) {
				for (SystemLoginUserPermission per : SystemLoginUserPermission.loginUserOperatePermssion) {
					//System.err.print("用户=" + per. () + ",接口地址= " + per.getUrl());

				}

			}
		}
		String requestParam = ParamUtil.getRequestParam(request);
		//获取session里的微信用户
		//Object object =request.getSession().getAttribute("member");
		SundayMemberSessionDto member= AccountIdHoldUtils.getSessionMember(request);

		//处理微信逻辑，拦截器
		//用户为空，透过微信获取用户信息
		if(member==null){
			if(url.startsWith("/zhongbao/weixin/")
					&&!url.startsWith("/sunday/weixin/safe/oAuth/")
					&&!url.startsWith("/zhongbao/weixin/session/")){
				logger.info("--------微信-拦截器-用户为空-开始执行");

				url+="?"+requestParam;
				//把当前的地址也作为参数，发到oAuth接口下
				if(org.apache.commons.lang3.StringUtils.isNotEmpty(requestParam)){
					requestParam+="&oldUrl="+url;

				}else{
					requestParam+="?oldUrl+="+url;
				}
				String oAuthUrl="/sunday/weixin/safe/oAuth?type=999&"+requestParam;

				logger.info("--------微信-拦截器-用户为空-开始执行-转跳授权地址"+oAuthUrl);
				response.sendRedirect(oAuthUrl);
				return false;
			}

		//当前用户不为空。判断用户状态。转入不同的页面
		}/*else{
			if(url.startsWith("/zhongbao/weixin/")
					&&!url.startsWith("/zhongbao/weixin/error")
					&&!url.startsWith("/sunday/weixin/safe/oAuth/")
					&&!url.startsWith("/zhongbao/weixin/session/")
					&&!url.startsWith("/zhongbao/weixin/register/")
					&&!url.startsWith("/zhongbao/weixin/pay/")){
				//用户状态，0正常，1冻结，2未注册，3待续费。
				Integer memberStatus=member.getStatus();
				String newUrl="";
				//正常，转入首页
				*//*if(memberStatus== SundayCommonConstants.user_status_zc){
					//response.sendRedirect("/zhongbao/weixin/index");
					newUrl="/zhongbao/weixin/index";
					//未注册。转入注册页
				}else*//*
				if(memberStatus== SundayCommonConstants.user_status_wzc){
					if(member.getUserType()==SundayCommonConstants.user_type_member){
						newUrl="/zhongbao/weixin/register/member/step1";
					}else{
						newUrl="/zhongbao/weixin/register/business/step1";
					}
				//	response.sendRedirect(newUrl);
					//return  false;
					//未续费
				}else if(memberStatus== SundayCommonConstants.user_status_dxf){
					newUrl="/zhongbao/weixin/pay/index";
				//	response.sendRedirect(newUrl);
				//	return  false;
				}else if(memberStatus== SundayCommonConstants.user_status_dj){
					newUrl="/zhongbao/weixin/error";

				}
				logger.info(newUrl);
				if(org.apache.commons.lang3.StringUtils.isNotEmpty(newUrl)){
					response.sendRedirect(newUrl);
					return  false;
				}else{
					return super.preHandle(request, response, handler);
				}

			}

			return super.preHandle(request, response, handler);
		}
*/


        return super.preHandle(request, response, handler);
    }




}
